package com.cuckoo.cuckoo_app2.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.cuckoo.cuckoo_app2.R;
import com.cuckoo.cuckoo_app2.dao.PlatList;
import com.cuckoo.cuckoo_app2.ui.PlatformActivity;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PlatformToListAdpter extends RecyclerView.Adapter<PlatformToListAdpter.ViewHolder> {

    private Context context;
    private PlatformActivity.ButtonClickListener buttonClickListener;
    private List<PlatList> dataList;
    private int color_success, color_fail;

    public PlatformToListAdpter(Context context, List<PlatList> dataList, PlatformActivity.ButtonClickListener buttonClickListener) {
        this.context = context;
        this.buttonClickListener = buttonClickListener;
        this.dataList = dataList;

        this.color_success = ContextCompat.getColor(context, R.color.success);
        this.color_fail = ContextCompat.getColor(context, R.color.fail);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.platform_list_compon, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlatList platList = dataList.get(position);
        Log.i("TEST", platList.getTitle());

        holder.p_title.setText(platList.getTitle());
        Picasso.get().load(platList.getImageUrl()).placeholder(R.drawable.ic_launcher_foreground).into(holder.p_imageUrl);

        if (platList.getStart().equals(1)) {
            holder.p_buttom.setText("移出");
            holder.p_buttom.setBackgroundColor(color_fail);
        } else {
            holder.p_buttom.setText("添加");
            holder.p_buttom.setBackgroundColor(color_success);
        }

        // 事件
        holder.p_buttom.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (platList.getStart().equals(1)) {
                    buttonClickListener.onRemove(platList);
                } else {
                    buttonClickListener.onAdd(platList);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    // 清空数据并通知更新
    public void clearData() {
        this.dataList.clear(); // 假设 mData 不是 null
        notifyDataSetChanged();
    }

    // 更新数据列表的方法
    public void updateData(List<PlatList> dataList) {
        this.dataList = dataList; // 直接替换数据列表
        notifyDataSetChanged(); // 通知RecyclerView数据已更改
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView p_imageUrl;
        TextView p_title;
        Button p_buttom;

        public ViewHolder(View itemView) {
            super(itemView);
            p_imageUrl = itemView.findViewById(R.id.p_imageUrl);
            p_title = itemView.findViewById(R.id.p_title);
            p_buttom = itemView.findViewById(R.id.p_buttom);
        }
    }

}
